package nina.yiyou.attendance.s10merge.start

import nina.yiyou.attendance.s10merge.compute.AttendDetailMerger
import nina.yiyou.attendance.s10merge.input.AttendLeaveExcelReader
import nina.yiyou.attendance.s10merge.input.AttendPunchExcelV2Reader
import nina.yiyou.attendance.s10merge.output.MergeResultGenerator
import nina.yiyou.attendance.s20sum.start.AttendStep20Runner
import nina.yiyou.performance.s20confirm.input.BasicExcelReaderV2
import nina.yiyou.salary.input.InfoExcelCollector
import nina.yiyou.salary.input.config.ConfigExcelReader
import nina.yiyou.salary.input.util.InputPathResolver
import nina.yiyou.salary.process.global.GlobalResultMaker

import java.nio.file.Path

class AttendStep10Runner {

  AttendStep10Runner(Path workshopPath) {
    _workshopPath = workshopPath
  }

  void run() {
    def config = ConfigExcelReader.create(_workshopPath).read()
    Map globalMap = config.global()

    def (Map basicMap) = new InfoExcelCollector([
        ['in.花名册', { String p -> new BasicExcelReaderV2(p).read() }],
    ], globalMap, _workshopPath).collect()

    println('读取考勤输入...')
    def pathVal = { String k -> InputPathResolver.create(_workshopPath, globalMap, k).resolve() }
//    List punchList = new AttendPunchExcelReader(pathVal('in.考勤打卡')).read()
    List punchList = new AttendPunchExcelV2Reader(pathVal('in.考勤打卡2')).read()
    Map leaveMap = new AttendLeaveExcelReader(pathVal('in.考勤请假')).read()

    println('生成考勤结果...')
    def globalResult = new GlobalResultMaker(globalMap, _workshopPath).make()
    List attendanceResult = makeMergeResult(punchList, leaveMap, basicMap, config, globalResult)

    println('导出csv...')
    new MergeResultGenerator(_workshopPath, attendanceResult, globalResult.toMap()).generate()

    // 功能多合一
    new AttendStep20Runner(attendanceResult, globalResult).run()
//    new AttendStep30Runner(attendanceResult, globalResult).run()
  }

  private List makeMergeResult(List punchV2, Map leaveMap, Map basicMap, ConfigExcelReader.Result config, GlobalResultMaker.Result globalCfg) {
    return new AttendDetailMerger(punchV2, leaveMap, basicMap, config.workTime(), globalCfg).merge()
  }

  private final Path _workshopPath
}
